package com.taa.controller;

import com.taa.dto.UserLoginDTO;
import com.taa.dto.UserRegisterDTO;
import com.taa.enums.ResponseCode;
import com.taa.exception.AppException;
import com.taa.response.Response;
import com.taa.service.IUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: taa
 * @Desc: 用户接口
 * @Create: 2025-06-22 15:36
 **/
@Slf4j // 添加日志注解
@RestController
@RequestMapping("/user") // 对应网关转发后的路径 /api/user/**
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 用户注册接口
     * @param registerDTO 注册信息
     * @return Response<Boolean>
     */
    @PostMapping("/register")
    public Response<Boolean> register(@RequestBody UserRegisterDTO registerDTO) {
        try {
            log.info("用户注册请求开始, username: {}", registerDTO.getUsername());
            Boolean success = userService.register(registerDTO);
            // 业务成功，构建成功响应
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(success)
                    .build();
        } catch (AppException e) {
            // 捕获已知业务异常
            log.error("用户注册业务异常, username: {}", registerDTO.getUsername(), e);
            return Response.<Boolean>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .build();
        } catch (Exception e) {
            // 捕获未知系统异常
            log.error("用户注册系统异常, username: {}", registerDTO.getUsername(), e);
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 用户登录接口
     * @param loginDTO 登录信息
     * @return Response<Boolean>
     */
    @PostMapping("/login")
    public Response<Boolean> login(@RequestBody UserLoginDTO loginDTO) {
        try {
            log.info("用户登录请求开始, username: {}", loginDTO.getUsername());
            Boolean success = userService.login(loginDTO);
            // 业务成功，构建成功响应
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(success)
                    .build();
        } catch (AppException e) {
            // 捕获已知业务异常
            log.error("用户登录业务异常, username: {}", loginDTO.getUsername(), e);
            return Response.<Boolean>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .build();
        } catch (Exception e) {
            // 捕获未知系统异常
            log.error("用户登录系统异常, username: {}", loginDTO.getUsername(), e);
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
}


